// index.js
// 获取应用实例
const app = getApp()

import { 
  indexSwiper,
  indexTopMv,
  indexCasePic,
  indexCaseMv
} from '../../api/index'

import { 
  info
 } from '../../api/about'
 
function Animation(){
  return wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: 1000,
    timingFunction: "ease",
    delay: 0
   })
}

const duration = 400
let caseBar_1 = new Animation()
let caseBar_2 = new Animation()
let caseInfo = new Animation()

caseBar_2.scaleX(0).step({ duration: 1000 })

// pages/price/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperImgs:[],
    navs:[
      { url:'/pages/tech/index', icon:'../../img/i1.png', title:'认证技师'},
      { url:'/pages/search/index', icon:'../../img/i2.png', title:'质保查询'},
      { url:'baoxian', icon:'../../img/i3.png', title:'车衣保险'},
      // { url:'/pages/admin/index', icon:'../../img/i4.png', title:'管理登录'},
      { url:'/pages/admin/login', icon:'../../img/i4.png', title:'管理登录'},
      { url:'shop', icon:'../../img/i5.png', title:'商城'},
    ],
    topMvSrc:'',
    caseNav:[{ title:'图片案例',bar:{} },{ title:'视频案例',bar:caseBar_2.export() }],
    caseIndex:0,
    animationCase:{},
    casePic:[],
    caseMv:[],
    galleryList:[],
    galleryCurrent:"0",
    galleryShow: false,
    baoxianAppid:'',
    baoxianPath:'',
    shopAppid:'',
    shopPath:''
  },
  hideGalleryFn(){
    this.setData({
      galleryCurrent:"0",
    })
  },
  showGalleryFn(e){
    let { imgs } = e.currentTarget.dataset
    imgs = imgs.map(item=>item.url)
    console.log(imgs)
    if(imgs.length){
      this.setData({
        galleryList:imgs,
        galleryShow:true
      })
    }
  },
  topNavFn(e){
    const { url } = e.currentTarget.dataset
    const { baoxianAppid,baoxianPath,shopAppid,shopPath } = this.data
    if(url.includes('/pages/')){
      wx.navigateTo({ url })
      return
    }else if(url.includes('baoxian')){
      console.log('保险')
      if(!baoxianAppid || !baoxianPath){
        wx.showToast({
          title: '跳转小程序未配置',
          icon: 'none',
        })
      }
      wx.navigateToMiniProgram({
        appId: baoxianAppid,
        path: baoxianPath,
        envVersion: 'release'
      })
    }else {
      console.log('商城')
      if(!shopAppid || !shopPath){
        wx.showToast({
          title: '跳转小程序未配置',
          icon: 'none',
        })
      }
      wx.navigateToMiniProgram({
        appId: shopAppid,
        path: shopPath,
        envVersion: 'release'
      })
    }
  },
  caseNavFn(e){
    const {index} = e.target.dataset
    this.setData({ caseIndex:index })
  },
  animationCaseNav(index){
    let {caseNav} = this.data
    if(index){
      caseBar_1.scaleX(0).step({ duration })
      caseBar_2.scaleX(1).step({ duration })
    }else {
      caseBar_1.scaleX(1).step({ duration })
      caseBar_2.scaleX(0).step({ duration })
    }
    caseNav = caseNav.map((item,index)=>{
      item.bar = index ===0 ? caseBar_1.export() : caseBar_2.export() 
      return item
    })
    this.setData({ caseNav })
  },
  animationCaseInfo(index){
    if(index){
      caseInfo.translateX('-50%').step({ duration })
    }else {
      caseInfo.translateX(0).step({ duration })
    }
    this.setData({ animationCase:caseInfo.export() })
  },
  caseNavFn: function (e) {
    const {index} = e.target.dataset
    this.setData({ caseIndex:index })
    this.animationCaseNav(index)
    this.animationCaseInfo(index)
   },
   loginInitFn(){
    let { navs } = this.data
    wx.getStorage({
      key: 'login_msg',
      success: (res) => {
        let _data = res.data
        if(_data){
          _data = JSON.parse(_data)
          const { role } = _data
          console.log(role)
          if(role==='资料员'){
            navs.map((item)=>{
              if(item.title==='管理登录'){
                item.url = '/pages/admin/index'
              }
              return item
            })
            this.setData({ navs })
          }
        }
      }
    });
   },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loginInitFn()
    // 首页幻灯片
    indexSwiper().then(res=>{
      this.setData({ swiperImgs:res.data })
    })
    // 品牌视频
    indexTopMv().then(res=>{
      this.setData({ topMvSrc:res.data })
    })
    // 首页案例
    indexCasePic().then(res=>{
      this.setData({ casePic:res.data })
    })
    indexCaseMv().then(res=>{
      this.setData({ caseMv:res.data })
    })
    // 跳转小程序
    info().then(res=>{
      const { baoxianAppid,baoxianPath,shopAppid,shopPath } = res.data
      this.setData({
        baoxianAppid,baoxianPath,shopAppid,shopPath
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loginInitFn()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})